﻿<UserControl x:Class="FR.SAFTAnalyserLite.Views.InvoiceView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:v="clr-namespace:FR.SAFTAnalyserLite.SAFT.Validations"
             xmlns:conv="clr-namespace:FR.SAFTAnalyserLite.ValueConverters">
    <UserControl.Resources>
        <conv:GroupTotalSummaryConverter  x:Key="myConverter" />
        <CollectionViewSource x:Key="taxes" Source="{Binding Resumo, IsAsync=True}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Codigo"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </UserControl.Resources>

    <ScrollViewer VerticalScrollBarVisibility="Auto">
        <Grid ScrollViewer.CanContentScroll="True">
            <Grid.RowDefinitions>
                <RowDefinition Height="100"/>
                <RowDefinition Height="200"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="100"/>
                <RowDefinition Height="100"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="50*"/>
                <ColumnDefinition Width="50*"/>
            </Grid.ColumnDefinitions>

            <GroupBox Header="Cabeçalho" Grid.Column="0" Grid.Row="0">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="15*"/>
                        <ColumnDefinition Width="30*"/>
                        <ColumnDefinition Width="20*"/>
                        <ColumnDefinition Width="35*"/>
                    </Grid.ColumnDefinitions>

                    <TextBlock Text="Data:" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" />
                    <TextBlock Text="Data sistema:" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right" />
                    <TextBlock Text="Data do estado:" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right" />

                    <TextBox Text="{Binding CurrentInvoice.InvoiceDate, StringFormat=d}" Grid.Row="0" Grid.Column="1" Style="{StaticResource textbox}"/>
                    <TextBox Text="{Binding CurrentInvoice.SystemEntryDate, StringFormat=G}" Grid.Row="1" Grid.Column="1" Style="{StaticResource textbox}"/>
                    <TextBox Text="{Binding CurrentInvoice.DocumentStatus.InvoiceStatusDate}" Grid.Row="2" Grid.Column="1" Style="{StaticResource textbox}"/>

                    <TextBlock Text="Tipo Série/Número:" Grid.Row="0" Grid.Column="2" HorizontalAlignment="Right" />
                    <TextBlock Text="Estado:" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Right" />
                    <TextBlock Text="Razão:" Grid.Row="2" Grid.Column="2" HorizontalAlignment="Right" />

                    <TextBox Text="{Binding CurrentInvoice.InvoiceNo}" Grid.Row="0" Grid.Column="3" Style="{StaticResource textbox}"/>
                    <TextBox Text="{Binding CurrentInvoice.DocumentStatus.InvoiceStatus}" Grid.Row="1" Grid.Column="3" Style="{StaticResource textbox}"/>
                    <TextBox Text="{Binding CurrentInvoice.DocumentStatus.Reason}" Grid.Row="2" Grid.Column="3" Style="{StaticResource textbox}"/>
                </Grid>
            </GroupBox>

            <GroupBox Header="Cliente" Grid.Column="1" Grid.Row="0">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="15*"/>
                        <ColumnDefinition Width="85*"/>
                    </Grid.ColumnDefinitions>

                    <TextBlock Text="Nome:" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" />
                    <TextBlock Text="Contribuinte:" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right" />
                    <TextBlock Text="ID:" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right" />

                    <TextBox Text="{Binding InvoiceCustomer.CompanyName}" Grid.Row="0" Grid.Column="1" Style="{StaticResource textbox}"/>
                    <TextBox Text="{Binding InvoiceCustomer.CustomerTaxID}" Grid.Row="1" Grid.Column="1" Style="{StaticResource textbox}"/>
                    <TextBox Text="{Binding InvoiceCustomer.CustomerID}" Grid.Row="2" Grid.Column="1" Style="{StaticResource textbox}"/>
                </Grid>
            </GroupBox>

            <DockPanel Grid.Row="1" Grid.ColumnSpan="2">
                
                <Button Width="130" Height="25" ToolTip="Exportar linhas para Excel" Margin="5" HorizontalAlignment="Left" DockPanel.Dock="Top"
                        Command="{Binding DoOpenExcelCommand}">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="..\Resources\Images\excel.ico"/>
                        <TextBlock Text="Exportar" Padding="5,0,0,0"/>
                    </StackPanel>
                </Button>
                
                <DataGrid x:Name="dataGridInvoiceLines"
                      ItemsSource="{Binding CurrentInvoiceLines, IsAsync=True, NotifyOnTargetUpdated=True}">
                    <DataGrid.RowValidationRules>
                        <v:InvoiceLineValidationRule ValidationStep="ConvertedProposedValue" ValidatesOnTargetUpdated="True"/>
                    </DataGrid.RowValidationRules>

                    <DataGrid.RowValidationErrorTemplate>
                        <ControlTemplate>
                            <Grid Margin="0,-2,0,-2"
                              ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}},
                            Path=(Validation.Errors)[0].ErrorContent}">
                                <Ellipse StrokeThickness="0" Fill="Red" 
                                     Width="{TemplateBinding FontSize}" 
                                     Height="{TemplateBinding FontSize}" />
                            </Grid>
                        </ControlTemplate>
                    </DataGrid.RowValidationErrorTemplate>

                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Nº linha" Binding="{Binding LineNumber}" Width="60"/>
                        <DataGridTextColumn Header="Nº documento" Binding="{Binding InvoiceNo}"/>
                        <DataGridTextColumn Header="Código produto" Binding="{Binding ProductCode}" />
                        <DataGridTextColumn Header="Descrição produto" Binding="{Binding ProductDescription}" />
                        <DataGridTextColumn Header="Quantidade" Binding="{Binding Quantity}"/>
                        <DataGridTextColumn Header="Preço unitário" Binding="{Binding UnitPrice}" />
                        <DataGridTextColumn Header="Valor" Binding="{Binding Item}" />
                        <DataGridTextColumn Header="Desconto" Binding="{Binding SettlementAmount}" />
                        <DataGridTextColumn Header="Tipo operação" Binding="{Binding ItemElementName}" />
                        <DataGridTextColumn Header="Imposto" Binding="{Binding Tax.Item}"/>
                        <DataGridTextColumn Header="Isenção imposto" Binding="{Binding TaxExemptionReason}"  />
                        <DataGridTextColumn Header="Referência" Binding="{Binding References[0].Reference}"/>
                        <DataGridTextColumn Header="Razão" Binding="{Binding References[0].Reason}"/>
                        <DataGridTextColumn Header="Unidade medida" Binding="{Binding UnitOfMeasure}" />
                        <DataGridTextColumn Header="Data" Binding="{Binding TaxPointDate, IsAsync=True, StringFormat=d}"  />
                        <DataGridTextColumn Header="Descrição" Binding="{Binding Description}" />
                    </DataGrid.Columns>
                </DataGrid>
            </DockPanel>

            <GridSplitter Grid.Row="2" Grid.ColumnSpan="2" VerticalAlignment="Center" Background="DarkGray" ShowsPreview="True" Height="5" Width="Auto" HorizontalAlignment="Stretch" ResizeDirection="Rows" Margin="3" />

            <GroupBox Header="Resumo Iva" Grid.Row="3" Grid.Column="0" Grid.RowSpan="2">
                <DataGrid x:Name="dataGridTaxes" TabIndex="1" DockPanel.Dock="Bottom"
                      ItemsSource="{Binding Source={StaticResource taxes}}">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Taxa" Binding="{Binding Taxa}" />
                        <DataGridTextColumn Header="Codigo" Binding="{Binding Codigo}" />
                        <DataGridTextColumn Header="Incidencia" Binding="{Binding Incidencia, StringFormat=\{0:N\}}" Width="90"/>
                        <DataGridTextColumn Header="Valor" Binding="{Binding Valor, StringFormat=\{0:N\}}" Width="90"/>
                    </DataGrid.Columns>

                    <DataGrid.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.HeaderTemplate>
                                <DataTemplate>
                                    <StackPanel>
                                        <TextBlock Text="{Binding Path=Name}" />
                                    </StackPanel>
                                </DataTemplate>
                            </GroupStyle.HeaderTemplate>
                            <GroupStyle.ContainerStyle>
                                <Style TargetType="{x:Type GroupItem}">
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                                <Expander IsExpanded="True">
                                                    <Expander.Header>
                                                        <StackPanel Orientation="Horizontal">
                                                            <TextBlock Text="{Binding Path=Name}" />
                                                            <TextBlock Text=" ( " />
                                                            <TextBlock Text="{Binding Path=ItemCount}" />
                                                            <TextBlock Text=" ) " />
                                                            <TextBlock Text="{Binding Path=Items, Converter={StaticResource myConverter}}" HorizontalAlignment="Right"/>
                                                        </StackPanel>
                                                    </Expander.Header>
                                                    <ItemsPresenter />
                                                </Expander>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </GroupStyle.ContainerStyle>
                        </GroupStyle>
                    </DataGrid.GroupStyle>

                </DataGrid>
            </GroupBox>
            
            <GroupBox Header="Totais" Grid.Column="1" Grid.Row="3">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <TextBlock Text="Incidência" Grid.Row="0" Grid.Column="0" FontSize="16" Margin="3,3,0,0"/>
                    <TextBlock Text="Imposto" Grid.Row="0" Grid.Column="1" FontSize="16" Margin="0,3,0,0"/>
                    <TextBlock Text="Total" Grid.Row="0" Grid.Column="2" FontSize="16" Margin="0,3,0,0"/>

                    <TextBox Text="{Binding CurrentInvoice.DocumentTotals.NetTotal, StringFormat=C}" Grid.Row="1" Grid.Column="0" FontSize="18" Margin="3,0,0,0" Style="{StaticResource textbox}"/>
                    <TextBox Text="{Binding CurrentInvoice.DocumentTotals.TaxPayable, StringFormat=C}" Grid.Row="1" Grid.Column="1" FontSize="18" Style="{StaticResource textbox}"/>
                    <TextBox Text="{Binding CurrentInvoice.DocumentTotals.GrossTotal, StringFormat=C}" Grid.Row="1" Grid.Column="2" FontSize="18" Style="{StaticResource textbox}" />
                </Grid>
            </GroupBox>
            
            <GroupBox Header="Certificação" Grid.Column="1" Grid.Row="4">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="15*"/>
                        <ColumnDefinition Width="85*"/>
                    </Grid.ColumnDefinitions>

                    <TextBlock Text="Hash:" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" />
                    <TextBlock Text="Hash control:" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right" />

                    <TextBox Text="{Binding CurrentInvoice.Hash}" Grid.Row="0" Grid.Column="1" Style="{StaticResource textbox}"/>
                    <TextBox Text="{Binding CurrentInvoice.HashControl}" Grid.Row="1" Grid.Column="1" Style="{StaticResource textbox}"/>

                    <TextBox Text="{Binding StringCertificacao}" Grid.Row="2" Grid.ColumnSpan="2" Style="{StaticResource textbox}" />
                </Grid>
            </GroupBox>

            <GridSplitter Grid.Row="5" Grid.ColumnSpan="2" VerticalAlignment="Center" Background="DarkGray" ShowsPreview="True" Height="5" Width="Auto" HorizontalAlignment="Stretch" ResizeDirection="Rows" Margin="3" />

            <GroupBox Header="Local de carga" Grid.Row="6" Grid.Column="0">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="20*"/>
                        <ColumnDefinition Width="80*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>

                    <TextBlock Text="Data de receção:" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" />
                    <TextBlock Text="Morada detalhada:" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right" />
                    <TextBlock Text="Nº de polícia:" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right" />
                    <TextBlock Text="Localidade:" Grid.Row="3" Grid.Column="0" HorizontalAlignment="Right" />
                    <TextBlock Text="País:" Grid.Row="4" Grid.Column="0" HorizontalAlignment="Right" />
                    <TextBlock Text="Código postal:" Grid.Row="5" Grid.Column="0" HorizontalAlignment="Right" />
                    <TextBlock Text="Distrito:" Grid.Row="6" Grid.Column="0" HorizontalAlignment="Right" />
                    <TextBlock Text="Nome da rua:" Grid.Row="7" Grid.Column="0" HorizontalAlignment="Right" />

                    <TextBox Text="{Binding CurrentInvoice.ShipFrom.DeliveryDate, StringFormat=d}" Grid.Row="0" Grid.Column="1" Style="{StaticResource textbox}" />
                    <TextBox Text="{Binding CurrentInvoice.ShipFrom.Address.AddressDetail}" Grid.Row="1" Grid.Column="1" Style="{StaticResource textbox}" />
                    <TextBox Text="{Binding CurrentInvoice.ShipFrom.Address.BuildingNumber}" Grid.Row="2" Grid.Column="1" Style="{StaticResource textbox}" />
                    <TextBox Text="{Binding CurrentInvoice.ShipFrom.Address.City}" Grid.Row="3" Grid.Column="1" Style="{StaticResource textbox}" />
                    <TextBox Text="{Binding CurrentInvoice.ShipFrom.Address.Country}" Grid.Row="4" Grid.Column="1" Style="{StaticResource textbox}" />
                    <TextBox Text="{Binding CurrentInvoice.ShipFrom.Address.PostalCode}" Grid.Row="5" Grid.Column="1" Style="{StaticResource textbox}" />
                    <TextBox Text="{Binding CurrentInvoice.ShipFrom.Address.Region}" Grid.Row="6" Grid.Column="1" Style="{StaticResource textbox}" />
                    <TextBox Text="{Binding CurrentInvoice.ShipFrom.Address.StreetName}" Grid.Row="7" Grid.Column="1" Style="{StaticResource textbox}" />

                    <Grid Grid.Row="8" Grid.ColumnSpan="2">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="50*"/>
                            <ColumnDefinition Width="50*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        
                        <ListBox ItemsSource="{Binding CurrentInvoice.ShipFrom.DeliveryID}" Grid.Row="0" Grid.Column="0"/>
                        <ListBox ItemsSource="{Binding CurrentInvoice.ShipFrom.LocationID}" Grid.Row="0" Grid.Column="1"/>
                        <ListBox ItemsSource="{Binding CurrentInvoice.ShipFrom.WarehouseID}" Grid.Row="1" Grid.ColumnSpan="2"/>
                    </Grid>
                </Grid>
            </GroupBox>

            <GroupBox Header="Local de descarga" Grid.Row="6" Grid.Column="1">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="20*"/>
                        <ColumnDefinition Width="80*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>

                    <TextBlock Text="Data de entrega:" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" />
                    <TextBlock Text="Morada detalhada:" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right"/>
                    <TextBlock Text="Nº de polícia:" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right"/>
                    <TextBlock Text="Localidade:" Grid.Row="3" Grid.Column="0" HorizontalAlignment="Right"/>
                    <TextBlock Text="País:" Grid.Row="4" Grid.Column="0" HorizontalAlignment="Right"/>
                    <TextBlock Text="Código postal:" Grid.Row="5" Grid.Column="0" HorizontalAlignment="Right"/>
                    <TextBlock Text="Distrito:" Grid.Row="6" Grid.Column="0" HorizontalAlignment="Right"/>
                    <TextBlock Text="Nome da rua" Grid.Row="7" Grid.Column="0" HorizontalAlignment="Right"/>

                    <TextBox Text="{Binding CurrentInvoice.ShipTo.DeliveryDate, StringFormat=d}" Grid.Row="0" Grid.Column="1" Style="{StaticResource textbox}" />
                    <TextBox Text="{Binding CurrentInvoice.ShipTo.Address.AddressDetail}" Grid.Row="1" Grid.Column="1" Style="{StaticResource textbox}" />
                    <TextBox Text="{Binding CurrentInvoice.ShipTo.Address.BuildingNumber}" Grid.Row="2" Grid.Column="1" Style="{StaticResource textbox}" />
                    <TextBox Text="{Binding CurrentInvoice.ShipTo.Address.City}" Grid.Row="3" Grid.Column="1" Style="{StaticResource textbox}" />
                    <TextBox Text="{Binding CurrentInvoice.ShipTo.Address.Country}" Grid.Row="4" Grid.Column="1" Style="{StaticResource textbox}" />
                    <TextBox Text="{Binding CurrentInvoice.ShipTo.Address.PostalCode}" Grid.Row="5" Grid.Column="1" Style="{StaticResource textbox}" />
                    <TextBox Text="{Binding CurrentInvoice.ShipTo.Address.Region}" Grid.Row="6" Grid.Column="1" Style="{StaticResource textbox}" />
                    <TextBox Text="{Binding CurrentInvoice.ShipTo.Address.StreetName}" Grid.Row="7" Grid.Column="1" Style="{StaticResource textbox}" />

                    <Grid Grid.Row="8" Grid.ColumnSpan="2">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="50*"/>
                            <ColumnDefinition Width="50*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>

                        <ListBox ItemsSource="{Binding CurrentInvoice.ShipTo.DeliveryID}" Grid.Row="0" Grid.Column="0"/>
                        <ListBox ItemsSource="{Binding CurrentInvoice.ShipTo.LocationID}" Grid.Row="0" Grid.Column="1"/>
                        <ListBox ItemsSource="{Binding CurrentInvoice.ShipTo.WarehouseID}" Grid.Row="1" Grid.ColumnSpan="2"/>
                    </Grid>
                </Grid>
            </GroupBox>
        </Grid>
    </ScrollViewer>
</UserControl>
